package demo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ZSetOperations;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.Set;

//后续 Redis 测试的各种方法，都通过这个 Controller 提供的 http 接口来触发
@RequestMapping("/redis")
@RestController
public class RedisController {

    @Autowired
    private StringRedisTemplate redisTemplate;

    @RequestMapping("/string")
    @ResponseBody
    public String string(){

        redisTemplate.execute((RedisConnection connection ) -> {
            connection.flushAll();
            return null;
        });

        redisTemplate.opsForValue().set("key1", "11");
        redisTemplate.opsForValue().set("key2", "22");
        redisTemplate.opsForValue().set("key3", "33");
        redisTemplate.opsForValue().set("key4", "44");

        String value = redisTemplate.opsForValue().get("key1");
        System.out.println("value = " + value);

        return "OK";
    }


    @RequestMapping("/list")
    @ResponseBody
    public String list(){

        redisTemplate.execute((RedisConnection connection) ->{
            connection.flushAll();
            return null;
        });

        redisTemplate.opsForList().rightPush("key", "11");
        redisTemplate.opsForList().rightPush("key", "22");
        redisTemplate.opsForList().rightPush("key", "33");
        redisTemplate.opsForList().rightPush("key", "44");

        String value = redisTemplate.opsForList().leftPop("key");
        System.out.println("value = " + value);
        value = redisTemplate.opsForList().leftPop("key");
        System.out.println("value = " + value);
        value = redisTemplate.opsForList().leftPop("key");
        System.out.println("value = " + value);
        value = redisTemplate.opsForList().leftPop("key");
        System.out.println("value = " + value);
        value = redisTemplate.opsForList().leftPop("key");
        System.out.println("value = " + value);

        return "OK";
    }

    @RequestMapping("/set")
    @ResponseBody
    public String set(){

        redisTemplate.execute((RedisConnection connection) -> {
            connection.flushAll();
            return null;
        });

        redisTemplate.opsForSet().add("key", "1", "2", "3", "4");

        Set<String> result = redisTemplate.opsForSet().members("key");
        System.out.println("result = " + result);

        Boolean exists = redisTemplate.opsForSet().isMember("key", "1111");
        System.out.println("exists = " + exists);

        Long count = redisTemplate.opsForSet().size("key");
        System.out.println("count = " + count);

        redisTemplate.opsForSet().remove("key", "1", "2");

        result = redisTemplate.opsForSet().members("key");
        System.out.println("result = " + result);

        return "OK";
    }

    @RequestMapping("/hash")
    @ResponseBody
    public String hash(){

        redisTemplate.execute((RedisConnection connection) -> {
            connection.flushAll();
            return null;
        });

        redisTemplate.opsForHash().put("key", "f1", "11");
        redisTemplate.opsForHash().put("key", "f2", "22");
        redisTemplate.opsForHash().put("key", "f3", "33");
        redisTemplate.opsForHash().put("key", "f4", "44");

        String value = (String) redisTemplate.opsForHash().get("key", "f1");
        System.out.println("value = " + value);

        Boolean exists = redisTemplate.opsForHash().hasKey("key", "f1");
        System.out.println("exists = " + exists);

        redisTemplate.opsForHash().delete("key", "f1", "f2");

        Long size = redisTemplate.opsForHash().size("key");
        System.out.println("size = " + size);

        return "OK";
    }

    @RequestMapping("/zset")
    @ResponseBody
    public String zset(){

        redisTemplate.execute((RedisConnection connection) -> {
            connection.flushAll();
            return null;
        });

        redisTemplate.opsForZSet().add("key", "a", 10);
        redisTemplate.opsForZSet().add("key", "b", 20);
        redisTemplate.opsForZSet().add("key", "c", 30);
        redisTemplate.opsForZSet().add("key", "d", 40);

        Set<String> members = redisTemplate.opsForZSet().range("key", 0, -1);
        System.out.println("members = " + members);

        Set<ZSetOperations.TypedTuple<String>> membersWithScore = redisTemplate.opsForZSet().rangeWithScores("key", 0, -1);
        System.out.println("membersWithScore = " + membersWithScore);

        Double score = redisTemplate.opsForZSet().score("key", "a");
        System.out.println("score = " + score);

        redisTemplate.opsForZSet().remove("key", "a");

        Long size = redisTemplate.opsForZSet().size("key");
        System.out.println("size = " + size);

        Long rank = redisTemplate.opsForZSet().rank("key", "b");
        System.out.println("rank = " + rank);

        return "OK";
    }
}
